package org.fire.activity;

import org.fire.FireApplication;
import org.fire.R;
import org.fire.mina.MinaMessageHandler;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends BaseActivity {
	private static final String TAG = "MainActivity";

	private MinaMessageHandler handler = new MinaMessageHandler() {

		@Override
		protected void doHandleMessage(String message) {
			JSONTokener tokener = new JSONTokener(message);
			JSONObject jsonObj = null;
			try {
				jsonObj = (JSONObject) tokener.nextValue();

			} catch (JSONException e) {
				Log.e(TAG, e.getMessage());
				return;
			}

			String lv = null;
			String msg = null;
			try {
				lv = jsonObj.getString("level");
				msg = jsonObj.getString("msg");

			} catch (JSONException e) {
				if (Log.isLoggable(TAG, Log.DEBUG)) {
					Log.d(TAG, "mesage ignored: " + message);
				}

				return;
			}

			ImageView statusIcon = (ImageView) findViewById(R.id.statusIcon);
			if ("warning".equals(lv)) {
				if (soundWarning) {
					return;
				}

				playWarning();
				statusIcon.setImageResource(R.drawable.fire_warning);
				TranslateAnimation translateAnimation = new TranslateAnimation(
						-100, 100, 0, 0);
				translateAnimation.setDuration(200);
				translateAnimation.setRepeatCount(Animation.INFINITE);
				translateAnimation.setRepeatMode(Animation.REVERSE);
				statusIcon.setAnimation(translateAnimation);
				translateAnimation.startNow();

			} else if ("info".equals(lv)) {
				stopWarning();
				statusIcon.setImageResource(R.drawable.fire_normal);
				statusIcon.setAnimation(null);
			}

			TextView statusText = (TextView) findViewById(R.id.statusText);
			statusText.setText(msg != null ? msg : getResources().getString(
					R.string.main_text_no_message));
		}
	};

	private SoundPool soundPool;
	private boolean soundLoadReady;
	private int soundSourceId;
	private int soundSteamId;
	private boolean soundWarning;
	private boolean soundWarningStart;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 10);
		soundSourceId = soundPool.load(this, R.raw.beep, 0);
		soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
			public void onLoadComplete(SoundPool soundPool, int sampleId,
					int status) {
				MainActivity.this.soundLoadReady = true;
			}
		});
	}

	private void playWarning() {
		if (soundLoadReady) {
			if (soundWarningStart) {
				soundPool.resume(soundSteamId);

			} else {
				soundSteamId = soundPool.play(soundSourceId, 1, 1, 0, -1, 1);
				soundWarning = true;
				soundWarningStart = true;
			}
		}
	}

	private void stopWarning() {
		if (soundLoadReady) {
			soundPool.pause(soundSteamId);
			soundWarning = false;
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();

		FireApplication.getApplication(this).setMessageHandler(handler);
	}

	@Override
	protected void onDestroy() {

	}

	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	// // Inflate the menu; this adds items to the action bar if it is present.
	// getMenuInflater().inflate(R.menu.main, menu);
	// return true;
	// }
	//
	// @Override
	// public boolean onOptionsItemSelected(MenuItem item) {
	// // Handle action bar item clicks here. The action bar will
	// // automatically handle clicks on the Home/Up button, so long
	// // as you specify a parent activity in AndroidManifest.xml.
	// int id = item.getItemId();
	// if (id == R.id.action_settings) {
	// return true;
	// }
	// return super.onOptionsItemSelected(item);
	// }
}
